﻿// See https://aka.ms/new-console-template for more information

using static homework05.Order;
using Newtonsoft.Json;


namespace homework05
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("<------------------------------------------------->");
            Console.WriteLine("欢迎使用订单管理系统!");
            var service = new OrderService();
            var cargos = new Cargos();
            Order CurrentOrder = null;
            cargos.printCargos();
            Console.WriteLine("支持命令:");
            Console.WriteLine("new <name> : 新建用户");
            Console.WriteLine("chk <ID> : 切换到已存在的订单");
            Console.WriteLine("pri : 打印");
            Console.WriteLine("add <name> <number> : 添加 number 个 名为 name 商品");
            Console.WriteLine("del <ID> : 根据订单ID删除订单");
            Console.WriteLine("mod <name> <number> : 修改 ID订单 number 个 名为 name 商品");
            Console.WriteLine("sor *<field>* : 按照字段进行排序");
            Console.WriteLine("que <field> <value> : 按照字段进行查找");
            Console.WriteLine("exp : 导出并退出");
            Console.WriteLine("<------------------------------------------------->");
            try
            {
                service.Import();
                Console.WriteLine("成功读取了存档数据: ");
                Console.WriteLine(JsonConvert.SerializeObject(service.OrderList));
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("未找到存档数据。请检查xml文件或者使用new创建用户.");
            }
            while (true)
            {
                Console.Write("[~] >");
                string op = Console.ReadLine();
                if (op.Length == 0)
                {
                    continue;
                }

                string[] ops = op.Split(" ");

                switch (ops[0])
                {
                    case "new":
                        if (op.Length <= 3)
                        {
                            Console.WriteLine("请输入客户名。");
                        }
                        else
                        {
                            var o = new Order(op.Substring(4));
                            service.AddOrder(o);
                            CurrentOrder = o;
                            Console.WriteLine("系统已成功为你创建了新订单。\n订单号：{0}\n用户:{1}\n通过add操作添加订单明细吧。", o.ID, o.Customer);
                        }
                        break;

                    case "chk":
                        if (op.Length <= 3)
                        {
                            Console.WriteLine("请输入需要操作的订购单ID。");
                        }
                        else
                        {
                            try
                            {
                                var o = service.FindOrder(op.Substring(4));
                                CurrentOrder = o;
                                Console.WriteLine("切换到 ID: {0}", CurrentOrder.ID);
                            }
                            catch (OrderInvalidException e)
                            {
                                Console.WriteLine(e.Message);
                            }
                        }
                        break;

                    case "add":
                        if (CurrentOrder == null)
                        {
                            Console.WriteLine("错误！请先通过new操作创建订单后再尝试添加订单明细。");
                            break;
                        }
                        try
                        {
                            var t = op.Split(' ');
                            string pname = t[1];
                            int pnum = Convert.ToInt32(t[2]);
                            try
                            {
                                CurrentOrder.AddItem(pname, pnum);
                                Console.WriteLine("成功为当前订单添加了{0}个{1}。", pnum, pname);
                            }
                            catch (ItemExistsException)
                            {
                                Console.WriteLine("添加商品失败，当前商品已存在。");
                            }
                            catch (CargoNotExistsException)
                            {
                                Console.WriteLine("添加商品失败，尝试添加的商品不存在。");
                            }

                        }
                        catch (IndexOutOfRangeException)
                        {
                            Console.WriteLine("命令格式错误，请检查后再试");
                        }
                        break;

                    case "del":
                        if (op.Length <= 3)
                        {
                            Console.WriteLine("请输入要删除的订单号。");
                        }
                        else
                        {
                            try
                            {
                                service.DelOrder(op.Substring(4));
                            }
                            catch (OrderInvalidException)
                            {
                                Console.WriteLine("删除订单失败，请检查订单号是否正确。");
                            }
                        }
                        break;

                    case "mod":
                        try
                        {
                            var t = op.Split(' ');
                            string pname1 = t[1];
                            int pnum1 = Convert.ToInt32(t[2]);
                            try
                            {
                                service.ModifyOrder(CurrentOrder.ID, pname1, pnum1);
                                Console.WriteLine("修改成功。");
                            }
                            catch (OrderInvalidException)
                            {
                                Console.WriteLine("修改订单失败，请检查订单号是否正确。");
                            }
                            catch (ItemInvalidException)
                            {
                                Console.WriteLine("修改订单失败，请检查商品名称是否正确。");
                            }
                        }
                        catch (IndexOutOfRangeException)
                        {
                            Console.WriteLine("命令格式错误，请检查后再试");
                        }
                        break;

                    case "sot":
                        if (op.Length <= 3)
                        {
                            service.SortOrder("ID");
                            Console.WriteLine("订单排序完成。");
                        }
                        else
                        {
                            var orderby = op.Substring(4);
                            try
                            {
                                service.SortOrder(orderby);
                                Console.WriteLine("订单排序完成。");
                            }
                            catch (InvalidSortException)
                            {
                                Console.WriteLine("请输入正确的排序依据。");
                            }
                        }
                        break;

                    case "que":
                        try
                        {
                            var t = op.Split(' ');
                            string queryop = t[1];
                            string querysrc = t[2];
                            try
                            {
                                String re = service.Query(queryop, querysrc);
                                Console.WriteLine(re);
                            }
                            catch (InvalidQueryException)
                            {
                                Console.WriteLine("错误，请检查查询依据是否正确。");
                            }
                        }
                        catch (IndexOutOfRangeException)
                        {
                            Console.WriteLine("命令格式错误，请检查后再试");
                        }
                        break;

                    case "exp":
                        service.Export();
                        Console.WriteLine("导出成功! 准备退出程序.");
                        return;

                    case "pri":
                        Console.WriteLine(JsonConvert.SerializeObject(service.OrderList));
                        break;

                    default:
                        Console.WriteLine("请输入正确的操作指令。");
                        break;
                }

            }
        }
    }
}